/**
 * 
 */
package research.sbookmarking.input;

import org.connotea.*;
import org.restlet.data.*;

import org.afox.util.*;
import java.util.*;

/**
 * @author albert
 *
 */
public class BookmarkCommand extends Command {

	/**
	 * @param name
	 * @param command
	 * @param param
	 */
	
	//number of times get bookmarks 
	private int loop = 1;
	//the starting value to retrieve at
	private int start = 1;
	//limit
	private int limit = 100;
	
	public BookmarkCommand(String name, String className,
						   String command, List param, 
						   Integer start, Integer limit, Integer times) {
		super(name, className, command, param);

		loop = times.intValue();
		this.start = start.intValue();
		this.limit = limit.intValue();
	}

	/* (non-Javadoc)
	 * @see research.sbookmarking.input.Command#execute()
	 */
	public void execute() {
		Connotea database = new Connotea("alby1976","a1014leung");
		//getList of bookmarks
		List result = new ArrayList();
		String command = getCommand();
		List param = getParameter();
		String url = (String)param.get(0);
		Reference uri = new Reference(url);
		param.clear();
		ArrayList results = new ArrayList();
		
		for (int count=0; count < loop; count++) {
			try {
				param.add(uri);
				//param.add(new Integer(limit));
				//param.add(new Integer(start+(limit*count)));
				
				System.out.println(count+": method: "+ command+" param:"+param);
				results.add(RMethod.invoke(database,command,param));
				System.out.println(result);
				//System.out.println("URL: " + ((Bookmark)((List)result.get(0)).get(0)).getLink());
			}
			catch (RuntimeException e){
				System.err.println (e.getMessage());
				System.exit(-1);
			}
			param.clear();
		}
		//setResult(result);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Connotea database = new Connotea("alby1976","a1014leung");
			Reference ref = new Reference("http://www.connotea.org/data/user/alby1976");
			//System.out.println ("ref: "+ref.getHostDomain());
			List obj=(List)database.getBookmarks("alby1976");
			Bookmark aBookmark=(Bookmark)obj.get(0);
			System.out.println(aBookmark.getLink());
			obj = (List)database.getBookmarks(aBookmark.getLink());
			System.out.println(obj);
			//System.out.println(database.getBookmarks((Reference)aBookmark.getLink()));

			//String command = new String ("getBookmarks");
			//ArrayList param = new ArrayList();
			
			//param.add("http://www.connotea.org/data/uri/685b90ae66cfbc3fc8ebeed0a5def571");
			//System.out.println("method: "+ command+" param:"+param);
			//Object objs = (RMethod.invoke(database,command,param));
			//System.out.println(objs);
			//System.out.println("URL: " + ((Bookmark)((List)result.get(0)).get(0)).getLink());

		}
		catch (Exception e){
			System.out.println(e);

		}
	}

}
